package com.breezelin.freshmarket.activities;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.breezelin.fmlib.widgets.FMTipDialog;
import com.breezelin.freshmarket.BuildConfig;
import com.breezelin.freshmarket.R;
import com.breezelin.freshmarket.app.FMApplication;
import com.breezelin.freshmarket.fragments.CategoryFragment;
import com.breezelin.freshmarket.fragments.HomeFragment;
import com.breezelin.freshmarket.fragments.LoginFragment;
import com.breezelin.freshmarket.fragments.ProfileFragment;
import com.breezelin.freshmarket.fragments.SearchFragment;
import com.breezelin.freshmarket.utils.LocalDataUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,
		FMTipDialog.DialogListener {

	/**
	 * 登出action
	 */
	public static final String ACTION_LOGOUT = BuildConfig.APPLICATION_ID + ".action.ACTION_LOGOUT";

	/**
	 * 标题栏
	 */
	@SuppressWarnings("FieldCanBeLocal")
	private Toolbar toolbar;
	/**
	 * 抽屉
	 */
	private DrawerLayout drawerLayout;
	/**
	 * 主页按钮
	 */
	@SuppressWarnings("FieldCanBeLocal")
	private View homeBtn;
	/**
	 * 目录按钮
	 */
	@SuppressWarnings("FieldCanBeLocal")
	private View categoryBtn;
	/**
	 * 搜索按钮
	 */
	@SuppressWarnings("FieldCanBeLocal")
	private View searchBtn;
	/**
	 * 账户按钮
	 */
	@SuppressWarnings("FieldCanBeLocal")
	private View profileBtn;

	/**
	 * 主页
	 */
	private HomeFragment homeFragment;
	/**
	 * 目录页
	 */
	private CategoryFragment categoryFragment;
	/**
	 * 搜索页
	 */
	private SearchFragment searchFragment;
	/**
	 * 账户页
	 */
	private ProfileFragment profileFragment;
	/**
	 * 提示登录页
	 */
	private LoginFragment loginFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		bindView();
		openHome();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == LoginActivity.CODE_LOGIN && resultCode == RESULT_OK && FMApplication.isLogin) {
			// 当登录页面返回了结果，登录完成，则跳转至用户信息页
			openProfile();
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		if (intent.getAction().equals(ACTION_LOGOUT)) {
			// 如果接收了登出动作，则重新打开账户页面
			openProfile();
		}
	}

	@Override
	public void onBackPressed() {
		FMTipDialog dialog = new FMTipDialog(this, getString(R.string.dialog_quit), this);
		dialog.show();
	}

	/**
	 * 绑定视图
	 */
	private void bindView() {
		// 绑定视图
		toolbar = (Toolbar) findViewById(R.id.toolbar);
		drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
		homeBtn = findViewById(R.id.btn_home);
		categoryBtn = findViewById(R.id.btn_category);
		searchBtn = findViewById(R.id.btn_search);
		profileBtn = findViewById(R.id.btn_profile);

		// 按钮监听
		homeBtn.setOnClickListener(this);
		categoryBtn.setOnClickListener(this);
		searchBtn.setOnClickListener(this);
		profileBtn.setOnClickListener(this);

		// 设置标题栏
		setSupportActionBar(toolbar);
		// 返回键可用
		//noinspection ConstantConditions
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		// 抽屉的监听
		ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
				R.string.app_name, R.string.app_name) {
			@Override
			public void onDrawerOpened(View drawerView) {
				super.onDrawerOpened(drawerView);
			}

			@Override
			public void onDrawerClosed(View drawerView) {
				super.onDrawerClosed(drawerView);
			}
		};
		drawerToggle.syncState();
		drawerLayout.addDrawerListener(drawerToggle);
	}

	/**
	 * 打开主页
	 */
	private void openHome() {
		if (homeFragment == null) {
			homeFragment = new HomeFragment();
		}
		FragmentManager manager = getSupportFragmentManager();
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.replace(R.id.frame_content, homeFragment, HomeFragment.TAG);
		transaction.commit();
	}

	/**
	 * 打开目录页面
	 */
	private void openCateGory() {
		if (categoryFragment == null) {
			categoryFragment = new CategoryFragment();
		}
		FragmentManager manager = getSupportFragmentManager();
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.replace(R.id.frame_content, categoryFragment, CategoryFragment.TAG);
		transaction.commit();
	}

	/**
	 * 打开搜索页面
	 */
	private void openSearch() {
		if (searchFragment == null) {
			searchFragment = new SearchFragment();
		}
		FragmentManager manager = getSupportFragmentManager();
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.replace(R.id.frame_content, searchFragment, SearchFragment.TAG);
		transaction.commit();
	}

	/**
	 * 打开账户页面
	 */
	private void openProfile() {
		if (FMApplication.isLogin) {
			// 已登录，进入用户主页
			if (profileFragment == null) {
				profileFragment = new ProfileFragment();
			}
			FragmentManager manager = getSupportFragmentManager();
			FragmentTransaction transaction = manager.beginTransaction();
			transaction.replace(R.id.frame_content, profileFragment, ProfileFragment.TAG);
			transaction.commit();
		} else {
			// 未登录，进入有登录按钮的页面
			if (loginFragment == null) {
				loginFragment = new LoginFragment();
			}
			FragmentManager manager = getSupportFragmentManager();
			FragmentTransaction transaction = manager.beginTransaction();
			transaction.replace(R.id.frame_content, loginFragment, LoginFragment.TAG);
			transaction.commit();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_home:
				openHome();
				break;
			case R.id.btn_category:
				openCateGory();
				break;
			case R.id.btn_search:
				openSearch();
				break;
			case R.id.btn_profile:
				openProfile();
				break;
		}
		drawerLayout.closeDrawers();
	}

	// 退出提示框的回调
	@Override
	public void onButtonClick(boolean left) {
		if (!left) {
			// 删除token
			LocalDataUtil.getInstance().put(
					LocalDataUtil.KEY_SVR_TOKEN,
					""
			);
			// 进行退出
			finish();
		}
	}
}
